package com.foogui.service.impl;

import com.foogui.api.UserFeignClient;
import com.foogui.dto.AccountDTO;
import com.foogui.dubbo.AccountDubboService;
import com.foogui.service.OrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @DubboReference(check = false)
    private AccountDubboService accountDubboService;
    @Override
    public void create(String userId, String commodityCode, Integer count) {
        int orderMoney = count * 100;
        jdbcTemplate.update("insert t_order(user_id,commodity_code,count,amount) values(?,?,?,?)",
                new Object[] {userId, commodityCode, count, orderMoney});
//        feign调用
        userFeignClient.reduce(userId, orderMoney);
    }

    @Override
    public void createOfDubbo(String userId, String commodityCode, Integer count) {
        int orderMoney = count * 100;
        jdbcTemplate.update("insert t_order(user_id,commodity_code,count,amount) values(?,?,?,?)",
                new Object[] {userId, commodityCode, count, orderMoney});

        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setUserId(userId);
        accountDTO.setAmount(orderMoney);
//        dubbo调用
        accountDubboService.reduce(accountDTO);
    }
}
